package com.fh.service.building.room;

import java.util.List;

import javax.annotation.Resource;

import com.fh.entity.system.TreeNode;
import org.springframework.stereotype.Service;

import com.fh.dao.DaoSupport;
import com.fh.entity.Page;
import com.fh.util.PageData;

@Service("roomService")
public class RoomService {

	@Resource(name = "daoSupport")
	private DaoSupport dao;
	
	/*
	* 新增
	*/
	public void save(PageData pd)throws Exception{
		dao.save("RoomMapper.save", pd);
	}

	/*
	* 新增项目
	*/
	public void saveProject(PageData pd)throws Exception{
		dao.save("RoomMapper.saveProject", pd);
	}

	/*
	* 删除
	*/
	public void delete(PageData pd)throws Exception{
		dao.delete("RoomMapper.delete", pd);
	}

	/*
	* 修改
	*/
	public void edit(PageData pd)throws Exception{
		dao.update("RoomMapper.edit", pd);
	}

	/*
	*列表
	*/
	public List<PageData> list(Page page)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.datalistPage", page);
	}

	/*
	*列表
	*/
	public List<PageData> findItemByBuildingId(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.findItemByBuildingId", pd);
	}

	/*
	*列表(全部)
	*/
	public List<PageData> listAll(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.listAll", pd);
	}
	/*
	*楼层列表(全部)
	*/
	public List<PageData> listFloor(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.listFloor", pd);
	}
	/*
	*楼 已租面积
	*/
	public List<PageData> roomArea(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.roomArea", pd);
	}
	/*
	*楼 总 面积
	*/
	public List<PageData> buildingArea(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.buildingArea", pd);
	}
	
	/*
	* 通过id获取数据
	*/
	public PageData findById(PageData pd)throws Exception{
		return (PageData)dao.findForObject("RoomMapper.findById", pd);
	}

	/*
	* 通过ids获取水表数据
	*/
	public List<PageData> findHydropowerWaterByIds(String[] ArrayDATA_IDS)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.findHydropowerWaterByIds", ArrayDATA_IDS);
	}

	/*
	* 通过ids获取水表数据
	*/
	public List<PageData> findHydropowerElectricByIds(String[] ArrayDATA_IDS)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.findHydropowerElectricByIds", ArrayDATA_IDS);
	}


	/*
	 * 查询类型
	 */
	public List<PageData> listType(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.listType", pd);
	}


	/*
	* 批量删除
	*/
	public void deleteAll(String[] ArrayDATA_IDS)throws Exception{
		dao.delete("RoomMapper.deleteAll", ArrayDATA_IDS);
	}

	/*
	通过ROOM_ID查询水电表
	 */
	public PageData findRoomByRoomId(PageData pd)throws Exception{
		return (PageData)dao.findForObject("RoomMapper.findRoomByRoomId", pd);
	}
	public PageData findRoom(PageData pd)throws Exception{
		return (PageData)dao.findForObject("RoomMapper.findRoom", pd);
	}
	public List<PageData> findRoomHydropower(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.findRoomHydropower", pd);
	}
	/* ===============================app房间接口================================== */
	/*
	*列表
	*/
	public List<PageData> roomdetail(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.roomdetail", pd);
	}

	/*
	*房间总列表
	*/
	public List<PageData> roomdetail1(PageData pd)throws Exception{
		return (List<PageData>)dao.findForList("RoomMapper.roomdetail1", pd);
	}

	/*
       tree
        */
	public TreeNode recursiveTree(String id, PageData pd,List<PageData> list) throws Exception {
		TreeNode node = new TreeNode();
		pd.put("id", id);
		PageData pnode = findByHydroId(pd);
		node.setId(pnode.getString("ID"));
		node.setName(pnode.getString("CODE"));
		node.setPid(pnode.getString("PID"));
		node.setChecked(checkHasId(pnode.getString("ID"),list));
		pd.put("pid", id);
		List<PageData> childs = findByPid(pd);
		for (PageData child : childs) {
			TreeNode c = recursiveTree(child.getString("ID"), pd,list);
			node.getChildren().add(c);
		}
		return node;
	}
	/*
   * 通过id获取水电表信息
   */
	public PageData findByHydroId(PageData pd) throws Exception {
		return (PageData) dao.findForObject("RoomMapper.findByHydroId", pd);
	}
	/*
    * 通过PID获取水电表信息
     */
	public List<PageData> findByPid(PageData pd) throws Exception {
		return (List<PageData>) dao.findForList("RoomMapper.findByPid", pd);
	}

	//判断该节点是否选中
	public boolean checkHasId(String id,List<PageData> list){
		boolean b = false;
		if(list.size()>0){
			for (int i=0;i<list.size();i++){
				PageData data = list.get(i);
				if (id.equals(data.getString("CODEID"))){
					b = true;
				}
			}
		}
		return  b;
	}

	public String getOldDatas(List<PageData> list){
		String str = "";
		if(list.size()>0){
			for (int i=0;i<list.size();i++){
				if(i == 0){
					str = list.get(i).getString("CODEID");
				}else {
					str += "," + list.get(i).getString("CODEID");
				}

			}
		}
		return str;
	}
}

